﻿using System;

namespace SimpleORM
{
    public class DbCTypeConverter : TypeConverter
    {
        #region Methods

        [TypeConverter(typeof(float))]
        [TypeConverter(typeof(float?))]
        [TypeConverter(typeof(Single))]
        [TypeConverter(typeof(Single?))]
        public virtual object ToSingle(object source)
        {
            return Convert.ToSingle(source);
        }

        [TypeConverter(typeof(double))]
        [TypeConverter(typeof(double?))]
        public virtual object ToDouble(object source)
        {
            return Convert.ToDouble(source);
        }

        [TypeConverter(typeof(decimal))]
        [TypeConverter(typeof(decimal?))]
        public virtual object ToDecimal(object source)
        {
            return Convert.ToDecimal(source);
        }

        [TypeConverter(typeof(long))]
        [TypeConverter(typeof(long?))]
        public virtual object ToInt64(object source)
        {
            return Convert.ToInt64(source);
        }

        [TypeConverter(typeof(int))]
        [TypeConverter(typeof(int?))]
        public virtual object ToInt32(object source)
        {
            return Convert.ToInt32(source);
        }

        [TypeConverter(typeof(short))]
        [TypeConverter(typeof(short?))]
        public virtual object ToInt16(object source)
        {
            return Convert.ToInt16(source);
        }

        [TypeConverter(typeof(bool))]
        [TypeConverter(typeof(bool?))]
        public virtual object ToBoolean(object source)
        {
            return Convert.ToBoolean(source);
        }

        [TypeConverter(typeof(string))]
        public virtual object ToString(object source)
        {
            if (source is string)
                return source;

            return source.ToString();
        }

        #endregion
    }
}